<?php

include dirname( __FILE__ )."/header.php";
$myts =& MyTextSanitizer::getInstance(); // MyTextSanitizer object
include_once dirname( __FILE__ )."/class/mydownloadstree.php";
$mytree = new MydownloadsTree($xoopsDB->prefix($mydirname."_cat"),"cid","pid");

$cid = isset($_GET['cid']) ? intval($_GET['cid']) : 0;
$catid = $cid;
$xoopsOption['template_main'] = $mydirname.'_viewcat.html';
include XOOPS_ROOT_PATH."/header.php";
//wanikoo
$mydownloads_module_header = '';
$mydownloads_module_header .= '
	<link rel="stylesheet" type="text/css" href="'.mydownloadsGetStyleURL("mydownloads.css").'" />
	<script src="'.mydownloadsGetStyleURL("mydownloads.js").'" type="text/javascript"></script>
	';
$mydownloads_module_header = $mydownloads_module_header; 
$xoopsTpl->assign('xoops_module_header', $mydownloads_module_header);
//

if (isset($_GET['show']) && $_GET['show']!="") {
    $show = intval($_GET['show']);
} else {
    $show = $xoopsModuleConfig['perpage'];
}
$min = isset($_GET['min']) ? intval($_GET['min']) : 0;
if (!isset($max)) {
    $max = $min + $show;
}
if(isset($_GET['orderby'])) {
    $orderby = convertorderbyin($_GET['orderby']);
} else {
    $orderby = "title ASC";
}

// list
if (isset($_GET['list']))
{
    $list = $_GET['list'];
    $orderby = "title ASC";
}
// list
if (!isset($_GET['list']))
{
	$xoopsTpl->assign('list_mode', false);
// list 

//wanikoo
$thiscattitle = "";
$result=$xoopsDB->query("SELECT title FROM ".$xoopsDB->prefix($mydirname."_cat")." WHERE cid = ".$cid."") or exit("Error");
$catrows = $xoopsDB->getRowsNum($result);
if ($catrows == 1) {
	while($myrow = $xoopsDB->fetchArray($result)) {
	$xoopsTpl->assign('thiscategorytitle', $myts->makeTboxData4Show($myrow['title']));
	$thiscattitle = $myts->makeTboxData4Show($myrow['title']);
	}
}
else {
$xoopsTpl->assign('thiscategorytitle', "");
}
if (file_exists(XOOPS_ROOT_PATH."/include/moremetasearch.php")&&$mydownloads_show_externalsearch) {
	include_once XOOPS_ROOT_PATH."/include/moremetasearch.php";
	$_REQUEST['query']= $thiscattitle;
	$engineblocktitle = _MD_MYDOWNLOADS_EXTERNALSEARCH;
	$engineblocktitle .= sprintf(_MD_MYDOWNLOADS_EXTERNALSEARCH_KEYWORD, _MD_MYDOWNLOADS_CATEGORY, $thiscattitle);
	$location_list=moremeta("meta_page","on");
	$metaresult = more_meta_page($location_list, $target="_blank", $display = false, $engineblocktitle);
	$xoopsTpl->assign('moremetasearch', "<br /><br />".$metaresult);
}
else {
	$xoopsTpl->assign('moremetasearch', '');
}
//feed
$xoopsTpl->assign('category_id', $cid);
$xoopsTpl->assign('lang_categoryfeed', _MD_MYDOWNLOADS_FEED_CAT);
//wanikoo end

$pathstring = "<a href='index.php'>"._MD_MYDOWNLOADS_MAIN."</a>&nbsp;:&nbsp;";
$pathstring .= $mytree->getNicePathFromId($cid, "title", "viewcat.php?op=");
$xoopsTpl->assign('category_path', $pathstring);
$xoopsTpl->assign('category_id', $cid);
// get child category objects
$arr=array();
$arr=$mytree->getFirstChild($cid, "title");
if ( count($arr) > 0 ) {
    $scount = 1;
    foreach($arr as $ele){
        $sub_arr=array();
        $sub_arr=$mytree->getFirstChild($ele['cid'], "title");
        $space = 0;
        $chcount = 0;
        $infercategories = "";
        foreach($sub_arr as $sub_ele){
            $chtitle=$myts->makeTboxData4Show($sub_ele['title']);
            if ($chcount>5){
                $infercategories .= "...";
                break;
            }
            if ($space>0) {
                $infercategories .= ", ";
            }
            $infercategories .= "<a href=\"".XOOPSMYDOWNLOADURL."/viewcat.php?cid=".$sub_ele['cid']."\">".$chtitle."</a>";
            $space++;
            $chcount++;
        }
        $xoopsTpl->append('subcategories', array('title' => $myts->makeTboxData4Show($ele['title']), 'id' => $ele['cid'], 'infercategories' => $infercategories, 'totallinks' => getTotalItems($ele['cid'], 1), 'count' => $scount));
        $scount++;
    }
}

// list end
}
else
{
	$xoopsTpl->assign('list_mode', true);
	$xoopsTpl->assign('category_path', sprintf(_MD_MYDOWNLOADS_DOWNLOADS_LIST, $_GET['list']));
}
// list end


if ($xoopsModuleConfig['useshots'] == 1) {
    $xoopsTpl->assign('shotwidth', $xoopsModuleConfig['shotwidth']);
    $xoopsTpl->assign('tablewidth', $xoopsModuleConfig['shotwidth'] + 10);
    $xoopsTpl->assign('show_screenshot', true);
    $xoopsTpl->assign('lang_noscreenshot', _MD_MYDOWNLOADS_NOSHOTS);
}

if (!empty($xoopsUser) && $xoopsUser->isAdmin($xoopsModule->mid())) {
    $isadmin = true;
} else {
    $isadmin = false;
}

// list
if (!isset($_GET['list']))
{
// list 
$fullcountresult=$xoopsDB->query("SELECT COUNT(*) FROM ".$xoopsDB->prefix($mydirname."_downloads")." WHERE cid=$cid AND status>0");
// list end
}
else
{
$fullcountresult=$xoopsDB->query("SELECT COUNT(*) FROM ".$xoopsDB->prefix($mydirname."_downloads")." WHERE title LIKE '".$myts->addSlashes($_GET['list'])."%' AND status>0");
}
// list end

list($numrows) = $xoopsDB->fetchRow($fullcountresult);
$page_nav = '';
if($numrows>0){
    $xoopsTpl->assign('lang_description', _MD_MYDOWNLOADS_DESCRIPTIONC);
    $xoopsTpl->assign('lang_lastupdate', _MD_MYDOWNLOADS_LASTUPDATEC);
    $xoopsTpl->assign('lang_hits', _MD_MYDOWNLOADS_HITSC);
    $xoopsTpl->assign('lang_ratingc', _MD_MYDOWNLOADS_RATINGC);
    $xoopsTpl->assign('lang_email', _MD_MYDOWNLOADS_EMAILC);
    $xoopsTpl->assign('lang_ratethissite', _MD_MYDOWNLOADS_RATETHISFILE);
    $xoopsTpl->assign('lang_reportbroken', _MD_MYDOWNLOADS_REPORTBROKEN);
    $xoopsTpl->assign('lang_tellafriend', _MD_MYDOWNLOADS_TELLAFRIEND);
    $xoopsTpl->assign('lang_modify', _MD_MYDOWNLOADS_MODIFY);
    $xoopsTpl->assign('lang_version' , _MD_MYDOWNLOADS_VERSION);
    $xoopsTpl->assign('lang_subdate' , _MD_MYDOWNLOADS_SUBMITDATE);
    $xoopsTpl->assign('lang_dlnow' , _MD_MYDOWNLOADS_DLNOW);
    $xoopsTpl->assign('lang_category' , _MD_MYDOWNLOADS_CATEGORYC);
    $xoopsTpl->assign('lang_size' , _MD_MYDOWNLOADS_FILESIZE);
    $xoopsTpl->assign('lang_platform' , _MD_MYDOWNLOADS_SUPPORTEDPLAT);
    $xoopsTpl->assign('lang_homepage' , _MD_MYDOWNLOADS_HOMEPAGE);
    $xoopsTpl->assign('lang_comments' , _COMMENTS);
    $xoopsTpl->assign('show_links', true);

// list
if (!isset($_GET['list']))
{
// list 
    $q = "SELECT d.cid, d.lid, d.title, d.url, d.homepage, d.version, d.size, d.platform, d.logourl, d.status, d.date, d.hits, d.rating, d.votes, d.comments, t.description FROM ".$xoopsDB->prefix($mydirname."_downloads")." d, ".$xoopsDB->prefix($mydirname."_text")." t WHERE cid=".$cid." AND d.status>0 AND d.lid=t.lid ORDER BY ".$orderby."";
// list end
}
else
{
    $q = "SELECT d.cid, d.lid, d.title, d.url, d.homepage, d.version, d.size, d.platform, d.logourl, d.status, d.date, d.hits, d.rating, d.votes, d.comments, t.description FROM ".$xoopsDB->prefix($mydirname."_downloads")." d, ".$xoopsDB->prefix($mydirname."_text")." t WHERE d.title LIKE '".$myts->addSlashes($_GET['list'])."%' AND d.status>0 AND d.lid=t.lid ORDER BY title ASC";
}
// list end

    $result = $xoopsDB->query($q,$show,$min);

//if 2 or more items in result, show the sort menu
    if($numrows>1){
        $xoopsTpl->assign('show_nav', true);
        $orderbyTrans = convertorderbytrans($orderby);
        $xoopsTpl->assign('lang_sortby', _MD_MYDOWNLOADS_SORTBY);
        $xoopsTpl->assign('lang_title', _MD_MYDOWNLOADS_TITLE);
        $xoopsTpl->assign('lang_date', _MD_MYDOWNLOADS_DATE);
        $xoopsTpl->assign('lang_rating', _MD_MYDOWNLOADS_RATING);
        $xoopsTpl->assign('lang_popularity', _MD_MYDOWNLOADS_POPULARITY);
        $xoopsTpl->assign('lang_cursortedby', sprintf(_MD_MYDOWNLOADS_CURSORTBY, convertorderbytrans($orderby)));
    }
    while(list($cid, $lid, $dtitle, $url, $homepage, $version, $size, $platform, $logourl, $status, $time, $hits, $rating, $votes, $comments, $description)=$xoopsDB->fetchRow($result)) {
	// list
	if (!isset($_GET['list']))
	{
       $path = $mytree->getPathFromId($cid, "title");
        $path = substr($path, 1);
        $path = str_replace("/"," <img src='".mydownloadsGetIconURL("arrow.gif")."' border='0' alt='' /> ",$path);
	}
	else {
       $path = $mytree->getPathFromId($cid, "title");
        $path = substr($path, 1);
        $path = str_replace("/"," <img src='".mydownloadsGetIconURL("arrow.gif")."' border='0' alt='' /> ",$path);       
       $xoopsTpl->assign('show_nav', false);
	}
	// list end
         $rating = number_format($rating, 2);
        $dtitle = $myts->makeTboxData4Show($dtitle);
        $url = $myts->makeTboxData4Show($url);
        $homepage = $myts->makeTboxData4Show($homepage);
        $version = $myts->makeTboxData4Show($version);
        $size = PrettySize($myts->makeTboxData4Show($size));
        $platform = $myts->makeTboxData4Show($platform);
        $logourl = $myts->makeTboxData4Show($logourl);
        $datetime = formatTimestamp($time,"s");
        $description = $myts->makeTareaData4Show($description,0); //no html
        $new = newdownloadgraphic($time, $status);
        $pop = popgraphic($hits);
        if ($isadmin) {
            $adminlink = '<a href="'.XOOPSMYDOWNLOADURL.'/admin/index.php?lid='.$lid.'&amp;fct=mydownloads&amp;op=modDownload"><img src="'.mydownloadsGetIconURL("editicon.gif").'" border="0" alt="'._MD_MYDOWNLOADS_EDITTHISDL.'" /></a>';
        } else {
            $adminlink = '';
        }
        if ($votes == 1) {
            $votestring = _MD_MYDOWNLOADS_ONEVOTE;
        } else {
            $votestring = sprintf(_MD_MYDOWNLOADS_NUMVOTES,$votes);
        }
        //$xoopsTpl->append('file', array('id' => $lid, 'cid' => $cid, 'rating' => $rating,'title' => $dtitle.$new.$pop,'logourl' => $logourl,'updated' => $datetime,'description' => $description,'adminlink' => $adminlink,'hits' => $hits,'votes' => $votestring, 'comments' => $comments, 'platform' => $platform,'size'  => $size,'homepage' => $homepage,'version'  => $version,'category'  => $path,'lang_dltimes' => sprintf(_MD_MYDOWNLOADS_DLTIMES,$hits),'mail_subject' => rawurlencode(sprintf(_MD_MYDOWNLOADS_INTFILEFOUND,$xoopsConfig['sitename'])),'mail_body' => rawurlencode(sprintf(_MD_MYDOWNLOADS_INTFILEFOUND,$xoopsConfig['sitename']).':  '.XOOPSMYDOWNLOADURL.'/singlefile.php?cid='.$cid.'&lid='.$lid)));
	 //rateimg for wf-theme 
        $rateimgtmp = round(number_format($rating, 0) / 2);
        $rateimg = "rate$rateimgtmp.gif";
        $xoopsTpl->append('file', array('id' => $lid, 'cid' => $cid, 'rating' => $rating,'title' => $dtitle.$new.$pop,'logourl' => $logourl,'updated' => $datetime,'description' => $description,'adminlink' => $adminlink,'hits' => $hits,'votes' => $votestring, 'comments' => $comments, 'platform' => $platform,'size'  => $size,'homepage' => $homepage,'version'  => $version,'category'  => $path,'lang_dltimes' => sprintf(_MD_MYDOWNLOADS_DLTIMES,$hits),'mail_subject' => rawurlencode(sprintf(_MD_MYDOWNLOADS_INTFILEFOUND,$xoopsConfig['sitename'])),'mail_body' => rawurlencode(sprintf(_MD_MYDOWNLOADS_INTFILEFOUND,$xoopsConfig['sitename']).':  '.XOOPSMYDOWNLOADURL.'/singlefile.php?cid='.$cid.'&lid='.$lid),'rateimg' => $rateimg));

    }
    $orderby = convertorderbyout($orderby);
// for navi in case of list
$cid = $catid;
// new navi 
include_once dirname( __FILE__ )."/class/mydownloadspagenav.php";
if (!isset($_GET['list']))
{
$mydownloadsnav = new MydownloadsPageNav($numrows, $show, $min, 'min', 'cid='.$cid.'&amp;orderby='.$orderby.'&amp;show='.$show);
}
else {
$mydownloadsnav = new MydownloadsPageNav($numrows, $show, $min, 'min', 'list='.$list.'&amp;orderby='.$orderby.'&amp;show='.$show);
}
$page_nav .= $mydownloadsnav->renderNav($offset =5);
//$page_nav .= $mydownloadsnav->renderImageNav();
//$page_nav .= $mydownloadsnav->renderSelect();

}
$xoopsTpl->assign('page_nav', $page_nav);

//wanikoo theme changer
$xoopsTpl->assign("lang_themechanger", _MD_MYDOWNLOADS_THEMECHANGER);
$mymydownloadstheme_options = '';

    foreach ($GLOBALS['mydownloads_allowed_theme'] as $mymydownloadstheme) {
        $mymydownloadstheme_options .= '<option value="'.$mymydownloadstheme.'"';
        if ($mymydownloadstheme == $GLOBALS['mydownloads_theme']) {
            $mymydownloadstheme_options .= ' selected="selected"';
        }
        $mymydownloadstheme_options .= '>'.$mymydownloadstheme.'</option>';
    }

    $mydownloadstheme_select = '<select name="mydownloads_theme_select" onchange="submit();" size="1">'.$mymydownloadstheme_options.'</select>';

$xoopsTpl->assign("mydownloadsthemeoption", $mydownloadstheme_select);

//wanikoo search
if ( file_exists(XOOPS_ROOT_PATH."/language/".$xoopsConfig['language']."/search.php") ) {
   include_once XOOPS_ROOT_PATH."/language/".$xoopsConfig['language']."/search.php";
} else {
   include_once XOOPS_ROOT_PATH."/language/english/search.php";
}
$xoopsTpl->assign('lang_all', _SR_ALL);
$xoopsTpl->assign('lang_any', _SR_ANY);
$xoopsTpl->assign('lang_exact', _SR_EXACT);
$xoopsTpl->assign('lang_search', _SR_SEARCH);
$xoopsTpl->assign('module_id', $xoopsModule->getVar('mid'));

//wanikoo wf_theme
if ( $mydownloads_wf_theme === true ) {
$xoopsTpl->assign('wf_theme', true);
}
else {
$xoopsTpl->assign('wf_theme', false);
}
//wanikoo wf
$catarray = array();
if ( $mydownloads_show_letters ) {
$catarray['letters'] = md_wfd_letters();
}
if ( $mydownloads_show_toolbar ) {
$catarray['toolbar'] = md_wfd_toolbar();
}
$xoopsTpl->assign('catarray', $catarray);
//pagetitle
$xoopsTpl->assign('xoops_pagetitle', $xoopsModule->getVar('name').' - '.$thiscattitle);
//category jump box
$catjumpbox = "<form name='catjumpbox' method='get' action='viewcat.php'>";
$catjumpbox .= "<b>"._MD_MYDOWNLOADS_CATEGORYC."</b>&nbsp;";
$catjumpbox .= $mytree->makeMySelBox2("title", "title", $cid);
$catjumpbox .= "&nbsp;<input type='submit' value='"._SUBMIT."' />\n</form>\n";
$xoopsTpl->assign('mydownloadsjumpbox', $catjumpbox);

include_once dirname( __FILE__ )."/footer.php";

?>